<?php
	function generateError($status,$msg){
		return array('status'=>$status,'message'=>$msg);
	}
	
	function isLocalhost() {
		$whitelist = array( '127.0.0.1', '::1' );
		if( in_array( $_SERVER['REMOTE_ADDR'], $whitelist) ){
			return true;
		}
		return false;
	}
	 function genVerificationCode($digits = 4) {
            return rand(pow(10, $digits - 1) - 1, pow(10, $digits) - 1);
        }
	function buildOutJobResults($result,$dbHelper,$con,$offset,array $attribues = array()){
		$respData=array();
		if($result){
			$locationId=array();
			while($row = mysqli_fetch_array($result)){
				$curId= $row['job_location_id'];
				array_push($locationId,$curId);
			}
			$jobResults=$dbHelper->getJobsByLocationIds($locationId,$con,$offset,$attribues);
			if($jobResults && mysqli_num_rows($jobResults)>0){
				$respData['status']=1;
				$respData['jobs']=array();
				while($jobRow=mysqli_fetch_array($jobResults)){
					$descrp=$jobRow['description'];
					$lat=$jobRow['latitude'];
					$lng=$jobRow['longitude'];
					$jobType=$jobRow['type'];
					$jobId=$jobRow['jobs_id'];
					$isImage=$jobRow['isImage'];
					$expireDate = $jobRow['added_date'];
					$addedDate = $jobRow['expire_date'];
					$city = $jobRow['city'];
					$state = $jobRow['statename'];
					$statecode = $jobRow['statecode'];
					$lastmodDate = $jobRow['last_mod_date'];
					$contactResults=$dbHelper->getContactInfoByJobId($jobId,$con);
					$contactInfo=array();
					if($contactResults){
						while($contactRow=mysqli_fetch_array($contactResults)){
							$contactValue=$contactRow['contact_value'];
							$contactType=$contactRow['contact_type_value'];
							array_push($contactInfo,array('contactType'=>$contactType,'contactValue'=>$contactValue));
						}
					}
					$singleResult = array("latitude"=>$lat,"longitude"=>$lng,"jobtype"=>$jobType,"isImage"=>$isImage);
					if($expireDate!=NULL){
						$singleResult['expireDate']=$expireDate;
					}
					if($addedDate!=NULL){
						$singleResult['addedDate']=$addedDate;
					}
					if($lastmodDate!=NULL){
						$singleResult['lastModDate']=$lastmodDate;
					}
					if($descrp!=NULL && $descrp!=""){
						$singleResult["description"]=$descrp;
					}
					$singleResult['city']=$city;
					$singleResult['state']=$state;
					$singleResult['statecode']=$statecode;
					if($isImage=='1'){
						$imageUrl=$jobRow['image_url'];
						$imageThumbnail = $jobRow['image_url_thumbnail'];
						if($imageUrl!=null){
							$singleResult['imageUrl']=$imageUrl;
						}
						if($imageThumbnail !=null){
							$singleResult['imageThumbnail']=$imageThumbnail;
						}
					}
					if(count($contactInfo)>0){
						$singleResult['isContact']='1';
						$singleResult['contacts']=$contactInfo;
						
					}else{
						$singleResult['isContact']='0';
					}
					array_push($respData['jobs'],$singleResult);
				}
			}else{
				$respData=generateError('0','Empty Job Result');
				//push out error
			}
			return $respData;
	}
	}
?>